package pro.spring.controller.frontController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pro.spring.model.Udiscuss;
import pro.spring.model.Udiscusser;
import pro.spring.vo.PaginationVo;

@Controller
@RequestMapping("fdis")
public class DisController extends BaseController{
	
	private Integer pagesize = 10 ;
	
	private String local = "page/front/disc/";
	
	@RequestMapping(value="taol.html",method=RequestMethod.GET)
	public String discussList(ModelMap map,String key){
		PaginationVo<Udiscusser> page = udiscusserService.getByNow(key,pagesize,1);
		map.put("pages",page);
		return local+"disc";
	}
	
	//我发布的
	@RequestMapping(value="fbed.html",method=RequestMethod.GET)
	public String dicyif(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		System.out.println("aaaaaaa"+uId);
		PaginationVo<Udiscusser> page = udiscusserService.getPageByUid(uId,pagenum, pagesize);
		map.put("pages",page);
		map.put("values","您还没有发布!");
		return local+"fbed";
	}
	
	//我参与的
	@RequestMapping(value="canyu.html",method=RequestMethod.GET)
	public String disccanyu(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		PaginationVo<Udiscusser> page = udiscusserService.getByCanyu(uId,pagenum, pagesize);
		map.put("pages",page);
		map.put("values","您还没有参与!");
		return local+"fbed";
	}
	
	
	@RequestMapping(value="fabu.html",method=RequestMethod.GET)
	public String dicfabu(ModelMap map){	
		return local+"fabu";
	}
	
	
	@ResponseBody
	@RequestMapping(value="add.json",method=RequestMethod.POST)
	public Integer addone(Udiscusser udis,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		udis.setUsersUId(uId);
		Integer num = udiscusserService.insert(udis);
		return num;
	}
	
	@ResponseBody
	@RequestMapping(value="del.json",method=RequestMethod.POST)
	public Integer delone(Integer id){
		Integer num = 0;
		num = udiscusserService.deleteByPrimaryKey(id);
		return num;
	}
	

	@ResponseBody
	@RequestMapping(value="xianadd.json",method=RequestMethod.POST)
	public Integer xianxia(Integer id,HttpServletRequest request,
			HttpServletResponse response){
		Integer num = 0 ;
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		Udiscuss u = new Udiscuss();
		u.setDsContent("我加入！");
		u.setUdiscusserDrId(id);
		u.setUsersUId(uId);
		num = udiscussService.insert(u);
		return num;
	}
	
	@ResponseBody
	@RequestMapping(value="xiandel.json",method=RequestMethod.POST)
	public Integer xiandel(Integer id,HttpServletRequest request,
			HttpServletResponse response){
		Integer num = 0 ;
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		num = udiscussService.deleteByXianxia(id, uId);
		return num;
	}
	

	
	@ResponseBody
	@RequestMapping(value="adddis.json",method=RequestMethod.POST)
	public Integer addonedis(Udiscuss udis,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		udis.setUsersUId(uId);
		Integer num = udiscussService.insert(udis);
		return num;
	}
	
	@RequestMapping(value="{id}/see.html",method=RequestMethod.GET)
	public String seeone(@PathVariable int id,ModelMap map,@RequestParam(value = "p", defaultValue = "10")Integer page,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		Udiscusser u = udiscusserService.selectByPrimaryKeys(uId,id,page);
		u.setUsers(usersService.selectByPrimaryKey(uId));
		Integer d = udiscusserService.selectCountBydrId(u.getDrId());
		if(page == null ){
			page = 10 ;
		}
		if(d>page){
			map.put("pageif",1);
		}else{
			map.put("pageif",0);
		}
		map.put("page",page);
		map.put("one",u);
		map.put("usid",uId);
		return local+"taoone";
	}
	
	
	@RequestMapping(value="xian/{id}.html",method=RequestMethod.GET)
	public String xian(@PathVariable int id,ModelMap map,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");	
		Udiscusser u = udiscusserService.selectByPrimaryKeyXianxia(id);
		int leng = u.getUli().size();
		Integer num = 0;
		for(int i=0;i<leng;i++){
			if(u.getUli().get(i).getuId()==uId){
				num = 1;
			}
		}
		if(uId==null){
			num=-1;
		}
		map.put("jiaru",num);
		map.put("one",u);
		return local+"xianxia";
	}
	
	@RequestMapping(value="reli.html",method=RequestMethod.GET)
	public String reMen(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,String key){
		System.out.println(key+"_key");
		PaginationVo<Udiscusser> page= udiscusserService.getByNow(key,pagesize,pagenum);
		map.put("key",key);
		map.put("pages",page);
		return local+"reli";
	}
	
	@RequestMapping(value="reli.html",method=RequestMethod.POST)
	public String re(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map,String key){
		System.out.println(key+"_key");
		PaginationVo<Udiscusser> page= udiscusserService.getByNow(key,pagesize,pagenum);
		map.put("key",key);
		map.put("pages",page);
		return local+"reli";
	}
}
